import Vue from 'vue'
import App from './App.vue'

import router from './router'

// 引入ui框架
import ElementUI from 'element-ui'
import 'element-ui/packages/theme-chalk/lib/index.css'

import {success_code} from "./utils/ResponseState";
import {role_admin} from "./utils/LRConstant";
import {checkToken} from "./api/userApi";

// configuration
Vue.use(ElementUI);
Vue.config.productionTip = false;

router.beforeEach((to, from, next) => {
    // check data
    // 如果是登录界面， 则需要放行
    if (to.path === "/login") {
        next();
    } else {
        // 检查角色1. admin放行 2. 游客跳转到门户首页
        checkToken().then(result => {

            if (result.code === success_code) {
                if (result.data.roles === role_admin) {
                    window.localStorage.setItem('avatar', result.data.avatar);
                    window.localStorage.setItem('userName', result.data.userName);
                    next()
                } else {
                    location.href = "http://localhost:8082";
                }
            } else {
                next({
                    path: '/login'
                })
            }
        });
    }
})
new Vue({
    router,
    render: h => h(App),
}).$mount('#app');
